package userlogic

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/login-rpc/internal/dao/model"
	"gitee.com/liuxuezhan/ar-platform/login-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/login-rpc/login_rpc"
	config "gitee.com/liuxuezhan/ar-platform/login-rpc/pkg/code"
	"gitee.com/liuxuezhan/ar-platform/login-rpc/pkg/tool"

	"github.com/zeromicro/go-zero/core/logc"
	"github.com/zeromicro/go-zero/core/logx"
)

type GoogleAuthenticationLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoogleAuthenticationLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoogleAuthenticationLogic {
	return &GoogleAuthenticationLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoogleAuthenticationLogic) GoogleAuthentication(in *login_rpc.GoogleAuthenticationRequest) (*login_rpc.GoogleAuthenticationResponse, error) {
	var commonResp login_rpc.CommonResp
	if in.Account == "" || in.LoginType == 0 {
		commonResp.Code = config.ParameterError
		commonResp.Message = "wrong parameter"
		return &login_rpc.GoogleAuthenticationResponse{CommonResp: &commonResp}, nil
	}

	var userId int64
	if in.LoginType == 1 {
		userId, _ = model.NewUserTotalModel(l.ctx, l.svcCtx.Db).GetUserByPhone(in.Account, in.PhoneCode)
	} else if in.LoginType == 2 {
		userId, _ = model.NewUserTotalModel(l.ctx, l.svcCtx.Db).GetUserByEmail(in.Account)
	} else {
		commonResp.Code = config.ParameterError
		commonResp.Message = "unsupported login type"
		return &login_rpc.GoogleAuthenticationResponse{CommonResp: &commonResp}, nil
	}

	if userId == 0 {
		commonResp.Code = config.ParameterError
		commonResp.Message = "account is not exist"
		return &login_rpc.GoogleAuthenticationResponse{CommonResp: &commonResp}, nil
	}

	user, err := model.NewUserModel(l.ctx, l.svcCtx.Db).GetGoogleByUserId(userId)
	if err != nil {
		commonResp.Code = config.TokenError
		commonResp.Message = "query failed"
		return &login_rpc.GoogleAuthenticationResponse{CommonResp: &commonResp}, nil
	}

	// 如果用户开启了谷歌登录，但没有谷歌二维码，则生成二维码 tody
	if user.IsGoogle == 1 {
		if user.GoogleCode == "" {
			user.GoogleCode, err = tool.GenGoogleAuthSecret()
			if err != nil {
				commonResp.Code = config.TokenError
				commonResp.Message = "failed to generate google auth secret"
				logc.Error(l.ctx, "GoogleAuthentication-GenGoogleAuthSecret,err:", err)
				return &login_rpc.GoogleAuthenticationResponse{CommonResp: &commonResp}, nil
			}

			// 更新到数据库
			err := model.NewUserModel(l.ctx, l.svcCtx.Db).UpdateUserGoogleCode(userId, user.GoogleCode)
			if err != nil {
				commonResp.Code = config.TokenError
				commonResp.Message = "update failed"
				return &login_rpc.GoogleAuthenticationResponse{CommonResp: &commonResp}, nil
			}
		}

		// 通过密钥生成二维码
		user.GoogleCode = tool.BuildGoogleQR(user.GoogleCode)
	}

	return &login_rpc.GoogleAuthenticationResponse{CommonResp: &commonResp, IsBinding: user.IsBindingGoogle, IsOpen: user.IsGoogle, QrCode: user.GoogleCode}, nil
}
